<?php
/* 制作一个空图片
 */
function gd_load_empty($nW, $nH, $sTran='000000') {
	$hImg  = imagecreate($nW, $nH);
	$cTran = imagecolorallocatealpha($hImg, hexdec(substr($sTran,0,2)), hexdec(substr($sTran,2,2)), hexdec(substr($sTran,4,2)), 127); // 设置透明色
	return $hImg;
}

/* 加载一个png图片，支持透明
 */
function gd_load_png($sImgPath, &$nW, &$nH) {
	$hImg = imagecreatefrompng($sImgPath); // 得到图片
	imagealphablending($hImg, false);
	imagesavealpha($hImg, true); // 声明支持透明
	$nW = imagesx($hImg);
	$nH = imagesy($hImg);
	return $hImg;
}

/* 加载一个jpg图片，不支持透明
 */
function gd_load_jpg($sImgPath, &$nW, &$nH) {
	$hImg = imagecreatefromjpeg($sImgPath); // 得到图片
	$nW = imagesx($hImg);
	$nH = imagesy($hImg);
	return $hImg;
}

/* 将一个图片旋转
 */
function gd_rotate($hImg, $fDegree, &$nW, &$nH) {
	if (0 == $fDegree%360) {
		$hNew = $hImg;
	}
	else {
		$cTran = imagecolorallocatealpha($hImg, 0, 0, 0, 127); // 设置透明色
		$hNew = imagerotate($hImg, $fDegree, $cTran); // 用透明色旋转
		imagedestroy($hImg); // 释放内存
		imagesavealpha($hNew, true); // 声明支持透明
	}
	$nW = imagesx($hNew);
	$nH = imagesy($hNew);
	return $hNew;
}

/* 计算文字的尺寸
 * 返回：包框宽度，包框高度，字符左上角与包框左上角的X轴距离，字符左上角与包框左上角的Y轴距离
 */
function gd_calc_text_size($nSize, $fDegree, $sFont, $sText) {
	$sFont = __PHY_LIB."/font/{$sFont}.ttf";
	if (!is_file($sFont)) { return array(0,0,0,0); } // 如果字体文件不存在，返回0
	$rPos = imagettfbbox($nSize, $fDegree, $sFont, $sText);
	$nMinX = $nMaxX = $rPos[0];
	$nMinX = min($nMinX, $rPos[2]); $nMaxX = max($nMaxX, $rPos[2]);
	$nMinX = min($nMinX, $rPos[4]); $nMaxX = max($nMaxX, $rPos[4]);
	$nMinX = min($nMinX, $rPos[6]); $nMaxX = max($nMaxX, $rPos[6]);
	$nMinY = $nMaxY = $rPos[1];
	$nMinY = min($nMinY, $rPos[1]); $nMaxY = max($nMaxY, $rPos[1]);
	$nMinY = min($nMinY, $rPos[3]); $nMaxY = max($nMaxY, $rPos[3]);
	$nMinY = min($nMinY, $rPos[5]); $nMaxY = max($nMaxY, $rPos[5]);
	$nMinY = min($nMinY, $rPos[7]); $nMaxY = max($nMaxY, $rPos[7]);
	$nW = $nMaxX - $nMinX + 1; // 包框宽度
	$nH = $nMaxY - $nMinY + 1; // 包框高度
	$nDX = $rPos[6] - $nMinX; // 字符左上角与包框左上角的X轴距离
	$nDY = $rPos[7] - $nMinY; // 字符左上角与包框左上角的Y轴距离
	return array($nW, $nH, $nDX, $nDY);
}

/* 在一张图上加上一段文字
 * 参数：sColor是6位RGB色号，sFont是ttf文件名
 */
function gd_add_text($hImg, $nSize, $fDegree, $nPosX, $nPosY, $sColor, $sFont, $sText) {
	$sFont = __PHY_LIB."/font/{$sFont}.ttf";
	if (!is_file($sFont)) { return false; } // 如果字体文件不存在，返回失败
	$cColor = imagecolorallocate($hImg, hexdec(substr($sColor,0,2)), hexdec(substr($sColor,2,2)), hexdec(substr($sColor,4,2)));
	$rPos = imagettfbbox($nSize, $fDegree, $sFont, $sText); // 计算改字体的影响范围
	$r = imagettftext($hImg, $nSize, $fDegree, $nPosX-$rPos[6], $nPosY-$rPos[7], $cColor, $sFont, $sText);
	return $r;
}

/* 计算内接框的尺寸
 */
function gd_zoom_out($rOld, $rKuang) {
	list($nOldW, $nOldH) = $rOld;
	list($nKuangW, $nKuangH) = $rKuang;
	if ($nOldW < $nKuangW && $nOldH < $nKuangH) {
		$nNewW = $nOldW;
		$nNewH = $nOldH;
	}
	elseif ($nOldW/$nOldH > $nKuangW/$nKuangH) { // over width
		$nNewW = $nKuangW;
		$nNewH = $nKuangW*$nOldH/$nOldW;
	}
	else { // over height
		$nNewH = $nKuangH;
		$nNewW = $nKuangH*$nOldW/$nOldH;
	}
	return array(ceil($nNewW), ceil($nNewH));
}

?>
